package com.tongfusoft.document.web.controller.systemParam;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tongfuosft.document.system.service.IDocSystempParamService;
import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.document.entity.DictionaryClass;
import com.tongfusoft.document.entity.SystemParam;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.spring.web.core.controller.BaseController;
import com.tongfusoft.spring.web.core.controller.annotation.UrlDesc;
import com.tongfusoft.spring.web.core.data.DataTable;
import com.tongfusoft.spring.web.core.data.ResultModel;
import com.tongfusoft.utils.JsonUtils;

import net.sf.json.JSONObject;

@Controller
public class SystemParamController extends BaseController {

	@Autowired
	@Qualifier("docSystemParamService")
	public IDocSystempParamService systemParamService;
	
	
	@UrlDesc(value="系统参数列表页")
	@RequestMapping("/systemParam")
	public String dictPage(HttpServletRequest request, Model model) {

		model.addAttribute("mainPageKey", "systemParam");
		model.addAttribute("mainPage", "system/systemParam");

		return "base";
	}
	@UrlDesc(value="系统参数查询")
	@RequestMapping("/systemParam/query")
	@ResponseBody
	public DataTable<DictionaryClass> systemParamQuery(Model model, int length, int start, int draw,String extra_search) {

		DataTable rm = new DataTable();
		try {
			
			SystemParam systemParam=(SystemParam)JsonUtils.getObject4JsonString(extra_search, SystemParam.class);
			int pageIndex = (start / length)+1;
			PageInfo pageInfo = this.systemParamService.paramList(length, pageIndex, systemParam);

			rm.setDraw(draw);
			rm.setRecordsFiltered(pageInfo.getTotal());
			rm.setRecordsTotal(pageInfo.getTotal());
			rm.setData(pageInfo.getList());
		} catch (Exception e) {

			e.printStackTrace();
		}

		return rm;

	}
	
	@UrlDesc(value="系统参数新增")
	@RequestMapping("/systemParam/add")
	@ResponseBody
	public ResultModel add(SystemParam param) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			
			this.systemParamService.createParam(param, (Users)super.getUser());
		} catch(ServiceException se){
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	@UrlDesc(value="系统参数修改")
	@RequestMapping("/systemParam/edit")
	@ResponseBody
	public ResultModel edit(SystemParam param) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			
			this.systemParamService.updateParam(param, (Users)super.getUser());
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	@UrlDesc(value="系统参数删除")
	@RequestMapping("/systemParam/del")
	@ResponseBody
	public ResultModel del(SystemParam param) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			
			this.systemParamService.deleteParam(param, (Users)super.getUser());
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	@UrlDesc(value="系统参数详情")
	@RequestMapping("/systemParam/detail")
	@ResponseBody
	public ResultModel dictClassDetail(String systemId) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			
			rm.setData(this.systemParamService.getParamById(systemId));
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
}
